ember-primitives 0.46.0 → 0.47.1

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.
@@ -0,0 +1,36 @@
1
+ import { getPromiseState } from 'reactiveweb/get-promise-state';
2
+ import type { Newable } from './type-utils.ts';
3
+ /**
4
+ * Instantiates a class once per application instance.
5
+ *
6
+ *
7
+ */
8
+ export declare function createService<Instance extends object>(context: object, theClass: Newable<Instance> | (() => Instance)): Instance;
9
+ /**
10
+ * Lazily instantiate a service.
11
+ *
12
+ * This is a replacement / alternative API for ember's `@service` decorator from `@ember/service`.
13
+ *
14
+ * For example
15
+ * ```js
16
+ * import { service } from 'ember-primitives/service';
17
+ *
18
+ * const loader = () => {
19
+ * let module = await import('./foo/file/with/class.js');
20
+ * return () => new module.MyState();
21
+ * }
22
+ *
23
+ * class Demo extends Component {
24
+ * state = createAsyncService(this, loader);
25
+ * }
26
+ * ```
27
+ *
28
+ * The important thing is for repeat usage of `createAsyncService` the second parameter,
29
+ * (loader in this case), must be shared between all usages.
30
+ *
31
+ * This is an alternative to using `createStore` inside an await'd component,
32
+ * or a component rendered with [`getPromiseState`](https://reactive.nullvoxpopuli.com/functions/get-promise-state.getPromiseState.html)
33
+ * ```
34
+ */
35
+ export declare function createAsyncService<Instance extends object>(context: object, theClass: () => Promise<Newable<Instance> | (() => Instance)>): ReturnType<typeof getPromiseState<unknown, Instance>>;
36
+ //# sourceMappingURL=service.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"service.d.ts","sourceRoot":"","sources":["../src/service.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,eAAe,EAAE,MAAM,+BAA+B,CAAC;AAKhE,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,iBAAiB,CAAC;AA6B/C;;;;GAIG;AACH,wBAAgB,aAAa,CAAC,QAAQ,SAAS,MAAM,EACnD,OAAO,EAAE,MAAM,EACf,QAAQ,EAAE,OAAO,CAAC,QAAQ,CAAC,GAAG,CAAC,MAAM,QAAQ,CAAC,GAC7C,QAAQ,CASV;AAID;;;;;;;;;;;;;;;;;;;;;;;;;GAyBG;AACH,wBAAgB,kBAAkB,CAAC,QAAQ,SAAS,MAAM,EACxD,OAAO,EAAE,MAAM,EACf,QAAQ,EAAE,MAAM,OAAO,CAAC,OAAO,CAAC,QAAQ,CAAC,GAAG,CAAC,MAAM,QAAQ,CAAC,CAAC,GAC5D,UAAU,CAAC,OAAO,eAAe,CAAC,OAAO,EAAE,QAAQ,CAAC,CAAC,CAgBvD"}
@@ -0,0 +1,89 @@
1
+
2
+ import { assert } from '@ember/debug';
3
+ import { getPromiseState } from 'reactiveweb/get-promise-state';
4
+ import { createStore } from './store.js';
5
+ import { findOwner } from './utils.js';
6
+
7
+ /*
8
+ import type { Newable } from './type-utils.ts';
9
+ import type { Registry } from '@ember/service';
10
+ import type Service from '@ember/service';
11
+
12
+ type Decorator = ReturnType<typeof emberService>;
13
+
14
+ // export function service<Key extends keyof Registry>(
15
+ // context: object,
16
+ // serviceName: Key
17
+ // ): Registry[Key] & Service;
18
+ export function service<Class extends object>(
19
+ context: object,
20
+ serviceDefinition: Newable<Class>
21
+ ): Class;
22
+ export function service<Class extends object>(serviceDefinition: Newable<Class>): Decorator;
23
+ export function service<Key extends keyof Registry>(serviceName: Key): Decorator;
24
+ export function service(prototype: object, name: string | symbol, descriptor: unknown): void;
25
+ export function service<Value, Result>(
26
+ context: object,
27
+ fn: Parameters<typeof getPromiseState<Value, Result>>[0]
28
+ ): ReturnType<typeof getPromiseState<Value, Result>>;
29
+ export function service<Value, Result>(
30
+ fn: Parameters<typeof getPromiseState<Value, Result>>[0]
31
+ ): Decorator;
32
+ */
33
+
34
+ /**
35
+ * Instantiates a class once per application instance.
36
+ *
37
+ *
38
+ */
39
+ function createService(context, theClass) {
40
+ const owner = findOwner(context);
41
+ assert(`Could not find owner / application instance. Cannot create a instance tied to the application lifetime without the application`, owner);
42
+ return createStore(owner, theClass);
43
+ }
44
+ const promiseCache = new WeakMap();
45
+
46
+ /**
47
+ * Lazily instantiate a service.
48
+ *
49
+ * This is a replacement / alternative API for ember's `@service` decorator from `@ember/service`.
50
+ *
51
+ * For example
52
+ * ```js
53
+ * import { service } from 'ember-primitives/service';
54
+ *
55
+ * const loader = () => {
56
+ * let module = await import('./foo/file/with/class.js');
57
+ * return () => new module.MyState();
58
+ * }
59
+ *
60
+ * class Demo extends Component {
61
+ * state = createAsyncService(this, loader);
62
+ * }
63
+ * ```
64
+ *
65
+ * The important thing is for repeat usage of `createAsyncService` the second parameter,
66
+ * (loader in this case), must be shared between all usages.
67
+ *
68
+ * This is an alternative to using `createStore` inside an await'd component,
69
+ * or a component rendered with [`getPromiseState`](https://reactive.nullvoxpopuli.com/functions/get-promise-state.getPromiseState.html)
70
+ * ```
71
+ */
72
+ function createAsyncService(context, theClass) {
73
+ let existing = promiseCache.get(theClass);
74
+ if (!existing) {
75
+ existing = async () => {
76
+ const result = await theClass();
77
+
78
+ // Pay no attention to the lies, I don't know what the right type is here
79
+ return createStore(context, result);
80
+ };
81
+ promiseCache.set(theClass, existing);
82
+ }
83
+
84
+ // Pay no attention to the TS inference crime here
85
+ return getPromiseState(existing);
86
+ }
87
+
88
+ export { createAsyncService, createService };
89
+ //# sourceMappingURL=service.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"service.js","sources":[],"sourcesContent":[],"names":[],"mappings":""}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ember-primitives",
3
- "version": "0.46.0",
3
+ "version": "0.47.1",
4
4
  "description": "Making apps easier to build",
5
5
  "keywords": [
6
6
  "ember-addon"
@@ -26,7 +26,7 @@
26
26
  "tabster": "^8.5.5",
27
27
  "tracked-built-ins": "^4.0.0",
28
28
  "tracked-toolbox": "^2.0.0",
29
- "which-heading-do-i-need": "0.1.0"
29
+ "which-heading-do-i-need": "0.2.0"
30
30
  },
31
31
  "devDependencies": {
32
32
  "@arethetypeswrong/cli": "^0.18.0",