@hkdigital/lib-sveltekit 0.0.31
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +104 -0
- package/dist/classes/index.d.ts +2 -0
- package/dist/classes/index.js +2 -0
- package/dist/classes/stores/SubscribersCount.d.ts +39 -0
- package/dist/classes/stores/SubscribersCount.js +107 -0
- package/dist/classes/stores/index.d.ts +1 -0
- package/dist/classes/stores/index.js +1 -0
- package/dist/classes/streams/LogTransformStream.d.ts +4 -0
- package/dist/classes/streams/LogTransformStream.js +19 -0
- package/dist/classes/streams/ServerEventsStore.d.ts +22 -0
- package/dist/classes/streams/ServerEventsStore.js +110 -0
- package/dist/classes/streams/TimeStampSource.d.ts +13 -0
- package/dist/classes/streams/TimeStampSource.js +26 -0
- package/dist/classes/streams/index.d.ts +3 -0
- package/dist/classes/streams/index.js +3 -0
- package/dist/components/area/HkArea.svelte +49 -0
- package/dist/components/area/HkArea.svelte.d.ts +12 -0
- package/dist/components/area/HkGridArea.svelte +77 -0
- package/dist/components/area/HkGridArea.svelte.d.ts +20 -0
- package/dist/components/area/index.d.ts +2 -0
- package/dist/components/area/index.js +2 -0
- package/dist/components/icon/HkIcon.svelte +86 -0
- package/dist/components/icon/HkIcon.svelte.d.ts +10 -0
- package/dist/components/icon/HkTabIcon.svelte +112 -0
- package/dist/components/icon/HkTabIcon.svelte.d.ts +19 -0
- package/dist/components/icon/index.d.ts +3 -0
- package/dist/components/icon/index.js +4 -0
- package/dist/components/icon/typedef.d.ts +13 -0
- package/dist/components/icon/typedef.js +16 -0
- package/dist/components/index.d.ts +1 -0
- package/dist/components/index.js +2 -0
- package/dist/components/layout/HkAppLayout.state.svelte.d.ts +6 -0
- package/dist/components/layout/HkAppLayout.state.svelte.js +25 -0
- package/dist/components/layout/HkAppLayout.svelte +251 -0
- package/dist/components/layout/HkAppLayout.svelte.d.ts +8 -0
- package/dist/components/layout/HkGridLayers.svelte +72 -0
- package/dist/components/layout/HkGridLayers.svelte.d.ts +20 -0
- package/dist/components/layout/index.d.ts +3 -0
- package/dist/components/layout/index.js +9 -0
- package/dist/components/tab-bar/HkTabBar.state.svelte.d.ts +54 -0
- package/dist/components/tab-bar/HkTabBar.state.svelte.js +149 -0
- package/dist/components/tab-bar/HkTabBar.svelte +74 -0
- package/dist/components/tab-bar/HkTabBar.svelte.d.ts +16 -0
- package/dist/components/tab-bar/HkTabBarSelector.state.svelte.d.ts +19 -0
- package/dist/components/tab-bar/HkTabBarSelector.state.svelte.js +93 -0
- package/dist/components/tab-bar/HkTabBarSelector.svelte +49 -0
- package/dist/components/tab-bar/HkTabBarSelector.svelte.d.ts +17 -0
- package/dist/components/tab-bar/index.d.ts +5 -0
- package/dist/components/tab-bar/index.js +17 -0
- package/dist/components/tab-bar/typedef.d.ts +6 -0
- package/dist/components/tab-bar/typedef.js +8 -0
- package/dist/constants/index.d.ts +2 -0
- package/dist/constants/index.js +2 -0
- package/dist/constants/regexp/index.d.ts +3 -0
- package/dist/constants/regexp/index.js +31 -0
- package/dist/constants/regexp/inspiratie.js__ +95 -0
- package/dist/constants/regexp/text.d.ts +47 -0
- package/dist/constants/regexp/text.js +49 -0
- package/dist/constants/regexp/user.d.ts +6 -0
- package/dist/constants/regexp/user.js +33 -0
- package/dist/constants/regexp/web.d.ts +1 -0
- package/dist/constants/regexp/web.js +3 -0
- package/dist/constants/time.d.ts +19 -0
- package/dist/constants/time.js +28 -0
- package/dist/css/test.css +3 -0
- package/dist/css/utilities.postcss +44 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +9 -0
- package/dist/server/index.d.ts +1 -0
- package/dist/server/index.js +1 -0
- package/dist/server/logger.d.ts +24 -0
- package/dist/server/logger.js +94 -0
- package/dist/states/index.d.ts +1 -0
- package/dist/states/index.js +1 -0
- package/dist/states/navigation.svelte.d.ts +23 -0
- package/dist/states/navigation.svelte.js +55 -0
- package/dist/stores/index.d.ts +1 -0
- package/dist/stores/index.js +1 -0
- package/dist/stores/theme.d.ts +28 -0
- package/dist/stores/theme.js +80 -0
- package/dist/util/expect/index.d.ts +39 -0
- package/dist/util/expect/index.js +145 -0
- package/dist/util/function.js__ +150 -0
- package/dist/util/index.d.ts +2 -0
- package/dist/util/index.js +2 -0
- package/dist/util/singleton/index.d.ts +34 -0
- package/dist/util/singleton/index.js +97 -0
- package/dist/util/svelte/index.d.ts +2 -0
- package/dist/util/svelte/index.js +2 -0
- package/dist/util/svelte/observe/index.d.ts +10 -0
- package/dist/util/svelte/observe/index.js +49 -0
- package/dist/util/svelte/state-context/index.d.ts +17 -0
- package/dist/util/svelte/state-context/index.js +83 -0
- package/dist/util/time.js__ +330 -0
- package/dist/valibot/index.d.ts +3 -0
- package/dist/valibot/index.js +30 -0
- package/dist/valibot/user.d.ts +6 -0
- package/dist/valibot/user.js +23 -0
- package/dist/zod/all.d.ts +6 -0
- package/dist/zod/all.js +33 -0
- package/dist/zod/generic.d.ts +6 -0
- package/dist/zod/generic.js +11 -0
- package/dist/zod/javascript.d.ts +8 -0
- package/dist/zod/javascript.js +32 -0
- package/dist/zod/user.d.ts +9 -0
- package/dist/zod/user.js +16 -0
- package/dist/zod/web.d.ts +21 -0
- package/dist/zod/web.js +57 -0
- package/package.json +144 -0
@@ -0,0 +1,145 @@
|
|
1
|
+
/**
|
2
|
+
* Validation functions that can be used as safe guards in your code
|
3
|
+
*
|
4
|
+
* @example
|
5
|
+
*
|
6
|
+
* import * as expect from '<path>/expect.js';
|
7
|
+
*
|
8
|
+
* function logText( text )
|
9
|
+
* {
|
10
|
+
* expect.string( text );
|
11
|
+
* expect.true( 1 > 0 );
|
12
|
+
*
|
13
|
+
* console.log( text );
|
14
|
+
* }
|
15
|
+
*
|
16
|
+
* logText( 'Hello' );
|
17
|
+
* logText( 123 ); // <- Will throw an exception
|
18
|
+
*/
|
19
|
+
|
20
|
+
import * as v from 'valibot';
|
21
|
+
|
22
|
+
/** Reusable schema's */
|
23
|
+
|
24
|
+
// const StringSchema = v.string();
|
25
|
+
// const FunctionSchema = v.function();
|
26
|
+
|
27
|
+
/** Exports */
|
28
|
+
|
29
|
+
// > Primitives
|
30
|
+
|
31
|
+
/**
|
32
|
+
* Throws a validation error if value is not a string
|
33
|
+
*
|
34
|
+
* @param {*} value
|
35
|
+
*/
|
36
|
+
export function string(value) {
|
37
|
+
v.parse(v.string(), value);
|
38
|
+
}
|
39
|
+
|
40
|
+
// boolean
|
41
|
+
// number
|
42
|
+
// symbol
|
43
|
+
// defined
|
44
|
+
|
45
|
+
// > Base objects
|
46
|
+
|
47
|
+
// object
|
48
|
+
|
49
|
+
// array
|
50
|
+
|
51
|
+
/**
|
52
|
+
* Throws a validation error if value is not an array of strings
|
53
|
+
*
|
54
|
+
* @param {*} value
|
55
|
+
*/
|
56
|
+
export function stringArray(value) {
|
57
|
+
v.parse(v.array(v.string()), value);
|
58
|
+
}
|
59
|
+
|
60
|
+
/**
|
61
|
+
* Throws a validation error if value is not an array of objects
|
62
|
+
*
|
63
|
+
* @param {*} value
|
64
|
+
*/
|
65
|
+
export function objectArray(value) {
|
66
|
+
v.parse(v.array(v.looseObject({})), value);
|
67
|
+
}
|
68
|
+
|
69
|
+
/**
|
70
|
+
* Throws a validation error if value is not a function
|
71
|
+
*
|
72
|
+
* @param {*} value
|
73
|
+
*/
|
74
|
+
function _function(value) {
|
75
|
+
v.parse(v.function(), value);
|
76
|
+
}
|
77
|
+
|
78
|
+
export { _function as function };
|
79
|
+
|
80
|
+
export { _function as class };
|
81
|
+
|
82
|
+
// promise
|
83
|
+
// map
|
84
|
+
// set
|
85
|
+
// error
|
86
|
+
|
87
|
+
// > Common values
|
88
|
+
|
89
|
+
// notNull
|
90
|
+
|
91
|
+
/**
|
92
|
+
* Expect a value to be a boolean and true
|
93
|
+
*
|
94
|
+
* @param {*} value
|
95
|
+
*/
|
96
|
+
export function _true(value) {
|
97
|
+
v.value(true, value);
|
98
|
+
}
|
99
|
+
|
100
|
+
export { _true as true };
|
101
|
+
|
102
|
+
// > Compounds
|
103
|
+
|
104
|
+
// positiveNumber
|
105
|
+
// notNegativeNumber
|
106
|
+
// positiveInteger
|
107
|
+
// notNegativeInteger
|
108
|
+
|
109
|
+
// stringOrNull
|
110
|
+
// stringOrUndefined
|
111
|
+
|
112
|
+
/**
|
113
|
+
* Throws a validation error if value is not a string
|
114
|
+
*
|
115
|
+
* @param {*} value
|
116
|
+
*/
|
117
|
+
export function notEmptyString(value) {
|
118
|
+
const schema = v.pipe(v.string(), v.minLength(1));
|
119
|
+
|
120
|
+
v.parse(schema, value);
|
121
|
+
}
|
122
|
+
|
123
|
+
// notEmptyStringOrNull
|
124
|
+
|
125
|
+
// asyncIterator
|
126
|
+
// iterable
|
127
|
+
// iterator
|
128
|
+
|
129
|
+
// store
|
130
|
+
|
131
|
+
// notEmptyArray
|
132
|
+
// arrayLike
|
133
|
+
// ArrayBuffer
|
134
|
+
// ArrayOfStrings
|
135
|
+
// arrayOrUndefined
|
136
|
+
// arangoCollectionId
|
137
|
+
// uriComponent
|
138
|
+
// objectNoArray
|
139
|
+
// objectNoFunction
|
140
|
+
// objectOrNull
|
141
|
+
// objectOrUndefined
|
142
|
+
// objectPath
|
143
|
+
// arrayOrSet
|
144
|
+
// setOfStrings
|
145
|
+
// emptyStringOrSymbol
|
@@ -0,0 +1,150 @@
|
|
1
|
+
/**
|
2
|
+
* function.js
|
3
|
+
*
|
4
|
+
* @description
|
5
|
+
* This file contains code for working with functions
|
6
|
+
*
|
7
|
+
* @example
|
8
|
+
*
|
9
|
+
* import { once } from "./function.js";
|
10
|
+
*
|
11
|
+
* const sayHelloOnce = once( () => { console.log("Hello"); } );
|
12
|
+
*
|
13
|
+
* sayHelloOnce();
|
14
|
+
* sayHelloOnce();
|
15
|
+
*/
|
16
|
+
|
17
|
+
/* ------------------------------------------------------------------ Imports */
|
18
|
+
|
19
|
+
import * as Types from '$lib/typedef/base.js';
|
20
|
+
|
21
|
+
/* ------------------------------------------------------------------ Exports */
|
22
|
+
|
23
|
+
/**
|
24
|
+
* 'No operation' function
|
25
|
+
* - A function that does nothing
|
26
|
+
*/
|
27
|
+
export const noop = () => {};
|
28
|
+
|
29
|
+
// -----------------------------------------------------------------------------
|
30
|
+
|
31
|
+
/**
|
32
|
+
* Wraps a function so that the callback function will be called only once
|
33
|
+
*
|
34
|
+
* @param {function} callback
|
35
|
+
*
|
36
|
+
* @returns {function} callback wrapped in `once` function
|
37
|
+
*/
|
38
|
+
export function once(callback) {
|
39
|
+
Types.Number(callback);
|
40
|
+
|
41
|
+
expectFunction(callback, 'Missing or invalid parameter [callback]');
|
42
|
+
|
43
|
+
let ignore = false;
|
44
|
+
|
45
|
+
return function () {
|
46
|
+
if (!ignore) {
|
47
|
+
ignore = true;
|
48
|
+
callback(...arguments);
|
49
|
+
}
|
50
|
+
};
|
51
|
+
}
|
52
|
+
|
53
|
+
// -----------------------------------------------------------------------------
|
54
|
+
|
55
|
+
/**
|
56
|
+
* Returns a debounced function
|
57
|
+
* - The original function is not called more than once during the
|
58
|
+
* specified interval
|
59
|
+
*
|
60
|
+
* @param {function} fn
|
61
|
+
* @param {number} [intervalMs=200]
|
62
|
+
*
|
63
|
+
* @returns {function} debounced function
|
64
|
+
*/
|
65
|
+
export function debounce(fn, intervalMs = 200) {
|
66
|
+
let idleTimer;
|
67
|
+
let lastArguments;
|
68
|
+
|
69
|
+
// console.log("debounce");
|
70
|
+
|
71
|
+
return function debounced() {
|
72
|
+
// console.log("debounced");
|
73
|
+
|
74
|
+
if (idleTimer) {
|
75
|
+
// console.log("idleTimer running");
|
76
|
+
|
77
|
+
// The function has been called recently
|
78
|
+
lastArguments = arguments;
|
79
|
+
return;
|
80
|
+
}
|
81
|
+
|
82
|
+
idleTimer = setTimeout(() => {
|
83
|
+
// console.log("idleTimer finished", lastArguments);
|
84
|
+
|
85
|
+
idleTimer = null;
|
86
|
+
|
87
|
+
if (lastArguments) {
|
88
|
+
//
|
89
|
+
// At least one call has been "debounced"
|
90
|
+
// -> make call with last arguments, so function always receives
|
91
|
+
// the arguments of the last call to the function
|
92
|
+
//
|
93
|
+
fn(...lastArguments);
|
94
|
+
lastArguments = undefined;
|
95
|
+
}
|
96
|
+
}, intervalMs);
|
97
|
+
|
98
|
+
fn(...arguments);
|
99
|
+
};
|
100
|
+
}
|
101
|
+
|
102
|
+
// -----------------------------------------------------------------------------
|
103
|
+
|
104
|
+
/**
|
105
|
+
* Adds a wrapper around a function that only calls the supplied function
|
106
|
+
* if the (first) supplied argument to the returned function is not `null`
|
107
|
+
*
|
108
|
+
* @param {object} [object]
|
109
|
+
* Optional function context to be used as `this`
|
110
|
+
*
|
111
|
+
* @param {function} functionOrMethodName
|
112
|
+
*
|
113
|
+
* @returns {function} not null wrapper function
|
114
|
+
*/
|
115
|
+
// export function ifNotNull( /* object, functionOrMethodName */ )
|
116
|
+
// {
|
117
|
+
// let fn;
|
118
|
+
|
119
|
+
// switch( arguments.length )
|
120
|
+
// {
|
121
|
+
// case 1:
|
122
|
+
// fn = arguments[0];
|
123
|
+
// expectFunction( fn, "Missing or invalid parameter [fn]" );
|
124
|
+
// break;
|
125
|
+
|
126
|
+
// case 2:
|
127
|
+
// {
|
128
|
+
// const object = arguments[0];
|
129
|
+
// const methodName = arguments[1];
|
130
|
+
|
131
|
+
// expectObject( object, "Invalid parameter [object]" );
|
132
|
+
// expectNotEmptyString( methodName, "Invalid parameter [methodName]" );
|
133
|
+
|
134
|
+
// fn = object[ methodName ].bind( object );
|
135
|
+
|
136
|
+
// expectFunction( fn, `Invalid method [<object>.${methodName}]` );
|
137
|
+
// }
|
138
|
+
// break;
|
139
|
+
|
140
|
+
// default:
|
141
|
+
// throw new Error("Invalid number of arguments");
|
142
|
+
// }
|
143
|
+
|
144
|
+
// return async ( value ) => {
|
145
|
+
// if( null !== value )
|
146
|
+
// {
|
147
|
+
// await fn( value );
|
148
|
+
// }
|
149
|
+
// };
|
150
|
+
// }
|
@@ -0,0 +1,34 @@
|
|
1
|
+
/**
|
2
|
+
* Get a singleton instance
|
3
|
+
* - A class can be supplied to create the singleton instance from
|
4
|
+
* - The singleton is identified by a needle string
|
5
|
+
*
|
6
|
+
* @param {object} _
|
7
|
+
* @param {*} _.class
|
8
|
+
* @param {*[]} [_.args] - Constructor arguments
|
9
|
+
* @param {string} [_.needle]
|
10
|
+
* If not supplied, a needle will be constructed from the serialized
|
11
|
+
* constructor arguments (args).
|
12
|
+
*/
|
13
|
+
export function getInstance({ needle, class: Class, args }: {
|
14
|
+
class: any;
|
15
|
+
args?: any[];
|
16
|
+
needle?: string;
|
17
|
+
}): any;
|
18
|
+
/**
|
19
|
+
* Creates a proxy that instantiates the singleton class upon it's first usage
|
20
|
+
*
|
21
|
+
* @template T
|
22
|
+
*
|
23
|
+
* @param {object} _
|
24
|
+
* @param {new() => T} _.class
|
25
|
+
* @param {*[]} [_.args] - Constructor arguments
|
26
|
+
*
|
27
|
+
* @returns {T & {init:()=>null}} proxy
|
28
|
+
*/
|
29
|
+
export function lazySingleton<T>({ class: Class, args }: {
|
30
|
+
class: new () => T;
|
31
|
+
args?: any[];
|
32
|
+
}): T & {
|
33
|
+
init: () => null;
|
34
|
+
};
|
@@ -0,0 +1,97 @@
|
|
1
|
+
import { expect } from '../index.js';
|
2
|
+
|
3
|
+
const instancesByNeedle = new Map();
|
4
|
+
|
5
|
+
/**
|
6
|
+
* Get a singleton instance
|
7
|
+
* - A class can be supplied to create the singleton instance from
|
8
|
+
* - The singleton is identified by a needle string
|
9
|
+
*
|
10
|
+
* @param {object} _
|
11
|
+
* @param {*} _.class
|
12
|
+
* @param {*[]} [_.args] - Constructor arguments
|
13
|
+
* @param {string} [_.needle]
|
14
|
+
* If not supplied, a needle will be constructed from the serialized
|
15
|
+
* constructor arguments (args).
|
16
|
+
*/
|
17
|
+
export function getInstance({ needle, class: Class, args }) {
|
18
|
+
expect.class(Class);
|
19
|
+
|
20
|
+
if (!needle) {
|
21
|
+
// Stringify instance arguments and use it as needle
|
22
|
+
needle = JSON.stringify(args === undefined ? null : args);
|
23
|
+
}
|
24
|
+
|
25
|
+
expect.notEmptyString(needle);
|
26
|
+
|
27
|
+
const className = Class.constructor.name;
|
28
|
+
|
29
|
+
// console.log('getInstance()', { needle, args, className });
|
30
|
+
|
31
|
+
needle = `${className}:${needle}`;
|
32
|
+
|
33
|
+
let instance = instancesByNeedle.get(needle);
|
34
|
+
|
35
|
+
if (!instance) {
|
36
|
+
// console.log('Create new instance');
|
37
|
+
// @ts-ignore
|
38
|
+
instance = new Class(...(args ?? []));
|
39
|
+
|
40
|
+
instancesByNeedle.set(needle, instance);
|
41
|
+
}
|
42
|
+
|
43
|
+
return instance;
|
44
|
+
}
|
45
|
+
|
46
|
+
/**
|
47
|
+
* Creates a proxy that instantiates the singleton class upon it's first usage
|
48
|
+
*
|
49
|
+
* @template T
|
50
|
+
*
|
51
|
+
* @param {object} _
|
52
|
+
* @param {new() => T} _.class
|
53
|
+
* @param {*[]} [_.args] - Constructor arguments
|
54
|
+
*
|
55
|
+
* @returns {T & {init:()=>null}} proxy
|
56
|
+
*/
|
57
|
+
export function lazySingleton({ class: Class, args }) {
|
58
|
+
/** @type {T} */
|
59
|
+
let singleton;
|
60
|
+
|
61
|
+
/** @type {T & {init:()=>null}} */
|
62
|
+
// @ts-ignore
|
63
|
+
const proxy = new Proxy(
|
64
|
+
// @ts-ignore
|
65
|
+
{},
|
66
|
+
{
|
67
|
+
/**
|
68
|
+
* @param {T} target
|
69
|
+
* @param {string} prop
|
70
|
+
*/
|
71
|
+
// @ts-ignore
|
72
|
+
get(target, prop) {
|
73
|
+
// console.log('lazySingleton: get property', prop);
|
74
|
+
|
75
|
+
if (!singleton) {
|
76
|
+
// @ts-ignore
|
77
|
+
singleton = new Class(...(args ?? []));
|
78
|
+
}
|
79
|
+
|
80
|
+
if (prop === 'init') {
|
81
|
+
// @ts-ignore
|
82
|
+
if (prop in singleton) {
|
83
|
+
// The class has an init method itself, return a reference
|
84
|
+
return singleton[prop];
|
85
|
+
}
|
86
|
+
|
87
|
+
return () => null;
|
88
|
+
}
|
89
|
+
|
90
|
+
// @ts-ignore
|
91
|
+
return singleton[prop];
|
92
|
+
}
|
93
|
+
}
|
94
|
+
);
|
95
|
+
|
96
|
+
return proxy;
|
97
|
+
}
|
@@ -0,0 +1,10 @@
|
|
1
|
+
/**
|
2
|
+
* @param {function} target
|
3
|
+
* @param {(entries:ResizeObserverEntry[]) => void} callback
|
4
|
+
* @param {object} [options]
|
5
|
+
*
|
6
|
+
* @returns {{ stop: () => void }}
|
7
|
+
*/
|
8
|
+
export function useResizeObserver(target: Function, callback: (entries: ResizeObserverEntry[]) => void, options?: object): {
|
9
|
+
stop: () => void;
|
10
|
+
};
|
@@ -0,0 +1,49 @@
|
|
1
|
+
import { onMount, onDestroy } from 'svelte';
|
2
|
+
|
3
|
+
/**
|
4
|
+
* @param {function} target
|
5
|
+
* @param {(entries:ResizeObserverEntry[]) => void} callback
|
6
|
+
* @param {object} [options]
|
7
|
+
*
|
8
|
+
* @returns {{ stop: () => void }}
|
9
|
+
*/
|
10
|
+
export function useResizeObserver(target, callback, options) {
|
11
|
+
/** @type {HTMLInputElement} */
|
12
|
+
let elem;
|
13
|
+
|
14
|
+
/** @type {ResizeObserver|undefined} */
|
15
|
+
let resizeObserver;
|
16
|
+
|
17
|
+
onMount(() => {
|
18
|
+
// console.log('useResizeObserver:onMount');
|
19
|
+
|
20
|
+
if (target instanceof HTMLInputElement) {
|
21
|
+
elem = target;
|
22
|
+
} else if (typeof target === 'function') {
|
23
|
+
let output = target();
|
24
|
+
|
25
|
+
// console.log({ output });
|
26
|
+
|
27
|
+
elem = output;
|
28
|
+
} else {
|
29
|
+
throw new Error('Invalid value for parameter [target]');
|
30
|
+
}
|
31
|
+
|
32
|
+
resizeObserver = new ResizeObserver(callback);
|
33
|
+
|
34
|
+
resizeObserver.observe(elem);
|
35
|
+
});
|
36
|
+
|
37
|
+
onDestroy(() => {
|
38
|
+
// console.log('useResizeObserver:onDestroy');
|
39
|
+
|
40
|
+
if (resizeObserver) {
|
41
|
+
resizeObserver.unobserve(elem);
|
42
|
+
resizeObserver = undefined;
|
43
|
+
}
|
44
|
+
});
|
45
|
+
|
46
|
+
return {
|
47
|
+
stop: () => {}
|
48
|
+
};
|
49
|
+
}
|
@@ -0,0 +1,17 @@
|
|
1
|
+
/**
|
2
|
+
* Define a state context:
|
3
|
+
* - Returns a function 'createOrGetState' that can be used to create a new state
|
4
|
+
* or get an existing one
|
5
|
+
* - Returns a function 'createState' that can be used to create a new state
|
6
|
+
*
|
7
|
+
* @template T
|
8
|
+
*
|
9
|
+
* @param {new() => T} State - Class that can be used to construct the state
|
10
|
+
*
|
11
|
+
* @returns {[
|
12
|
+
* createOrGetState,
|
13
|
+
* createState,
|
14
|
+
* getState
|
15
|
+
* ]}
|
16
|
+
*/
|
17
|
+
export function defineStateContext<T>(State: new () => T): [(instanceKey?: string | Symbol) => T, (instanceKey?: string | Symbol) => T, (instanceKey?: string | Symbol) => T];
|
@@ -0,0 +1,83 @@
|
|
1
|
+
import { setContext, getContext, hasContext } from 'svelte';
|
2
|
+
|
3
|
+
/* ----------------------------------------------------------- Create and Get */
|
4
|
+
|
5
|
+
/**
|
6
|
+
* Define a state context:
|
7
|
+
* - Returns a function 'createOrGetState' that can be used to create a new state
|
8
|
+
* or get an existing one
|
9
|
+
* - Returns a function 'createState' that can be used to create a new state
|
10
|
+
*
|
11
|
+
* @template T
|
12
|
+
*
|
13
|
+
* @param {new() => T} State - Class that can be used to construct the state
|
14
|
+
*
|
15
|
+
* @returns {[
|
16
|
+
* createOrGetState,
|
17
|
+
* createState,
|
18
|
+
* getState
|
19
|
+
* ]}
|
20
|
+
*/
|
21
|
+
export function defineStateContext(State) {
|
22
|
+
const description = State.name;
|
23
|
+
|
24
|
+
const sharedKey = Symbol(description);
|
25
|
+
|
26
|
+
/**
|
27
|
+
* Create component state
|
28
|
+
*
|
29
|
+
* @param {string|Symbol} [instanceKey]
|
30
|
+
*
|
31
|
+
* @returns {T} state
|
32
|
+
*/
|
33
|
+
function createState(instanceKey) {
|
34
|
+
const key = instanceKey ?? sharedKey;
|
35
|
+
|
36
|
+
// console.log('Create state', key);
|
37
|
+
|
38
|
+
const state = new State();
|
39
|
+
|
40
|
+
// @ts-ignore
|
41
|
+
state._instanceKey = instanceKey;
|
42
|
+
|
43
|
+
return setContext(key, state);
|
44
|
+
}
|
45
|
+
|
46
|
+
/**
|
47
|
+
* Get component state or create a new state if it does not yet exist
|
48
|
+
*
|
49
|
+
* @param {string|Symbol} [instanceKey]
|
50
|
+
*
|
51
|
+
* @returns {T} state
|
52
|
+
*/
|
53
|
+
function createOrGetState(instanceKey) {
|
54
|
+
let key = instanceKey ?? sharedKey;
|
55
|
+
|
56
|
+
if (!hasContext(key)) {
|
57
|
+
return createState(key);
|
58
|
+
}
|
59
|
+
|
60
|
+
return getContext(key);
|
61
|
+
}
|
62
|
+
|
63
|
+
/**
|
64
|
+
* Get component state
|
65
|
+
*
|
66
|
+
* @throws Will throw an error if the state-context does not exist
|
67
|
+
*
|
68
|
+
* @param {string|Symbol} [instanceKey]
|
69
|
+
*
|
70
|
+
* @returns {T} state
|
71
|
+
*/
|
72
|
+
function getState(instanceKey) {
|
73
|
+
let key = instanceKey ?? sharedKey;
|
74
|
+
|
75
|
+
if (!hasContext(key)) {
|
76
|
+
throw new Error(`No state context found. Create one first`);
|
77
|
+
}
|
78
|
+
|
79
|
+
return getContext(key);
|
80
|
+
}
|
81
|
+
|
82
|
+
return [createOrGetState, createState, getState];
|
83
|
+
}
|