add some types to `utils.ts`

pull/7324/head
Ivan Hofer 5 years ago
parent 281ee3fd91
commit 49b869b1dd

@ -50,6 +50,7 @@ export function invalidate(renderer: Renderer, scope: Scope, node: Node, names:
const extra_args = tail.map(variable => get_invalidated(variable)).filter(Boolean); const extra_args = tail.map(variable => get_invalidated(variable)).filter(Boolean);
if (is_store_value) { if (is_store_value) {
// TODO: check why there are 4 parameters, but `set_store_value` only expects 3
return x`@set_store_value(${head.name.slice(1)}, ${node}, ${head.name}, ${extra_args})`; return x`@set_store_value(${head.name.slice(1)}, ${node}, ${head.name}, ${extra_args})`;
} }

@ -1,13 +1,16 @@
import { Readable } from 'svelte/store'; import { Readable, Subscriber, Invalidator, Writable } from 'svelte/store';
import { SvelteComponent } from '..';
export function noop() { } export function noop() { }
export const identity = x => x; export function identity<T>(x: T): T {
return x;
}
export function assign<T, S>(tar: T, src: S): T & S { export function assign<T, S>(target: T, source: S): T & S {
// @ts-ignore // @ts-ignore
for (const k in src) tar[k] = src[k]; for (const k in source) target[k] = source[k];
return tar as T & S; return target as T & S;
} }
export function is_promise<T = any>(value: any): value is PromiseLike<T> { export function is_promise<T = any>(value: any): value is PromiseLike<T> {
@ -20,15 +23,17 @@ export function add_location(element, file, line, column, char) {
}; };
} }
export function run(fn) { export function blank_object(): {} {
return fn(); return Object.create(null);
} }
export function blank_object() { type FunctionWithoutArguments = () => unknown
return Object.create(null);
export function run(fn: FunctionWithoutArguments) {
return fn();
} }
export function run_all(fns: Function[]) { export function run_all(fns: FunctionWithoutArguments[]) {
fns.forEach(run); fns.forEach(run);
} }
@ -36,13 +41,9 @@ export function is_function(thing: any): thing is Function {
return typeof thing === 'function'; return typeof thing === 'function';
} }
export function safe_not_equal(a, b) { let src_url_equal_anchor: HTMLAnchorElement;
return a != a ? b == b : a !== b || ((a && typeof a === 'object') || typeof a === 'function');
}
let src_url_equal_anchor;
export function src_url_equal(element_src, url) { export function src_url_equal(element_src: string, url: string) {
if (!src_url_equal_anchor) { if (!src_url_equal_anchor) {
src_url_equal_anchor = document.createElement('a'); src_url_equal_anchor = document.createElement('a');
} }
@ -50,52 +51,55 @@ export function src_url_equal(element_src, url) {
return element_src === src_url_equal_anchor.href; return element_src === src_url_equal_anchor.href;
} }
export function not_equal(a, b) { export function safe_not_equal(a: unknown, b: unknown) {
return a != a ? b == b : a !== b || ((a && typeof a === 'object') || typeof a === 'function');
}
export function not_equal(a: unknown, b: unknown) {
return a != a ? b == b : a !== b; return a != a ? b == b : a !== b;
} }
export function is_empty(obj) { export function is_empty(obj: Record<string | number | symbol, unknown>) {
return Object.keys(obj).length === 0; return Object.keys(obj).length === 0;
} }
export function validate_store(store, name) { export function validate_store<S extends Readable<unknown>>(store: S, name: string) {
if (store != null && typeof store.subscribe !== 'function') { if (store != null && typeof store.subscribe !== 'function') {
throw new Error(`'${name}' is not a store with a 'subscribe' method`); throw new Error(`'${name}' is not a store with a 'subscribe' method`);
} }
} }
export function subscribe(store, ...callbacks) { export function subscribe<T, S extends Readable<T>>(store: S, run: Subscriber<T>, invalidate?: Invalidator<T>) {
if (store == null) { if (store == null) {
return noop; return noop;
} }
const unsub = store.subscribe(...callbacks); return store.subscribe(run, invalidate);
return unsub.unsubscribe ? () => unsub.unsubscribe() : unsub;
} }
export function get_store_value<T>(store: Readable<T>): T { export function get_store_value<T, S extends Readable<T>>(store: S): T {
let value; let value: T;
subscribe(store, _ => value = _)(); subscribe<T, S>(store, v => value = v)();
return value; return value;
} }
export function component_subscribe(component, store, callback) { export function component_subscribe<T, S extends Readable<T>>(component: SvelteComponent, store: S, callback: Subscriber<T>) {
component.$$.on_destroy.push(subscribe(store, callback)); component.$$.on_destroy.push(subscribe(store, callback));
} }
export function create_slot(definition, ctx, $$scope, fn) { export function create_slot(definition, ctx, $$scope, fn: Function) {
if (definition) { if (definition) {
const slot_ctx = get_slot_context(definition, ctx, $$scope, fn); const slot_ctx = get_slot_context(definition, ctx, $$scope, fn);
return definition[0](slot_ctx); return definition[0](slot_ctx);
} }
} }
function get_slot_context(definition, ctx, $$scope, fn) { function get_slot_context(definition, ctx, $$scope, fn: Function) {
return definition[1] && fn return definition[1] && fn
? assign($$scope.ctx.slice(), definition[1](fn(ctx))) ? assign($$scope.ctx.slice(), definition[1](fn(ctx)))
: $$scope.ctx; : $$scope.ctx;
} }
export function get_slot_changes(definition, $$scope, dirty, fn) { export function get_slot_changes(definition, $$scope, dirty, fn: Function) {
if (definition[2] && fn) { if (definition[2] && fn) {
const lets = definition[2](fn(dirty)); const lets = definition[2](fn(dirty));
@ -164,25 +168,27 @@ export function compute_slots(slots) {
return result; return result;
} }
export function once(fn) { export function once(fn: Function) {
let ran = false; let ran = false;
return function(this: any, ...args) { return function (this: any, ...args: unknown[]) {
if (ran) return; if (ran) return;
ran = true; ran = true;
fn.call(this, ...args); fn.call(this, ...args);
}; };
} }
export function null_to_empty(value) { export function null_to_empty<T>(value: T): T extends null | undefined ? '' : T {
return value == null ? '' : value; return (value == null ? '' : value) as T extends null | undefined ? '' : T;
} }
export function set_store_value(store, ret, value) { export function set_store_value<T, S extends Writable<T>>(store: S, ret: Node, value: T) {
store.set(value); store.set(value);
return ret; return ret;
} }
export const has_prop = (obj, prop) => Object.prototype.hasOwnProperty.call(obj, prop); export function has_prop<X extends {}, Y extends PropertyKey>(obj: X, prop: Y): obj is X & Record<Y, unknown> {
return Object.prototype.hasOwnProperty.call(obj, prop);
}
export function action_destroyer(action_result) { export function action_destroyer(action_result) {
return action_result && is_function(action_result.destroy) ? action_result.destroy : noop; return action_result && is_function(action_result.destroy) ? action_result.destroy : noop;

@ -10,7 +10,7 @@ export type Unsubscriber = () => void;
export type Updater<T> = (value: T) => T; export type Updater<T> = (value: T) => T;
/** Cleanup logic callback. */ /** Cleanup logic callback. */
type Invalidator<T> = (value?: T) => void; export type Invalidator<T> = (value?: T) => void;
/** Start and stop notification callbacks. */ /** Start and stop notification callbacks. */
export type StartStopNotifier<T> = (set: Subscriber<T>) => Unsubscriber | void; export type StartStopNotifier<T> = (set: Subscriber<T>) => Unsubscriber | void;

Loading…
Cancel
Save