@opra/core 0.31.5 → 0.31.6

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.
@@ -1,18 +1,40 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.ApiService = void 0;
4
+ const cachedServices = Symbol('cachedServices');
4
5
  class ApiService {
5
6
  get context() {
6
7
  if (!this._context)
7
8
  throw new Error(`No context assigned for ${Object.getPrototypeOf(this).constructor.name}`);
8
9
  return this._context;
9
10
  }
10
- forContext(context, options) {
11
- if (this._context === context && !options?.newInstance)
12
- return this;
11
+ forContext(arg0, attributes) {
12
+ let context;
13
+ if (arg0 instanceof ApiService) {
14
+ context = arg0.context;
15
+ attributes = arg0;
16
+ }
17
+ else
18
+ context = arg0;
19
+ const cacheMap = context[cachedServices] = context[cachedServices] || new WeakMap();
20
+ const ctor = Object.getPrototypeOf(this).constructor;
21
+ const cachedInstances = cacheMap.get(ctor) || [];
22
+ cacheMap.set(ctor, cachedInstances);
23
+ const newInstance = !this._context || this._context !== context;
24
+ if (!newInstance) {
25
+ const instance = cachedInstances
26
+ .find(service => this._cacheMatch(service, context, attributes));
27
+ if (instance)
28
+ return instance;
29
+ }
13
30
  const instance = { context };
14
31
  Object.setPrototypeOf(instance, this);
32
+ cachedInstances.push(instance);
15
33
  return instance;
16
34
  }
35
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
36
+ _cacheMatch(service, context, attributes) {
37
+ return service.context === context;
38
+ }
17
39
  }
18
40
  exports.ApiService = ApiService;
@@ -1,14 +1,36 @@
1
+ const cachedServices = Symbol('cachedServices');
1
2
  export class ApiService {
2
3
  get context() {
3
4
  if (!this._context)
4
5
  throw new Error(`No context assigned for ${Object.getPrototypeOf(this).constructor.name}`);
5
6
  return this._context;
6
7
  }
7
- forContext(context, options) {
8
- if (this._context === context && !options?.newInstance)
9
- return this;
8
+ forContext(arg0, attributes) {
9
+ let context;
10
+ if (arg0 instanceof ApiService) {
11
+ context = arg0.context;
12
+ attributes = arg0;
13
+ }
14
+ else
15
+ context = arg0;
16
+ const cacheMap = context[cachedServices] = context[cachedServices] || new WeakMap();
17
+ const ctor = Object.getPrototypeOf(this).constructor;
18
+ const cachedInstances = cacheMap.get(ctor) || [];
19
+ cacheMap.set(ctor, cachedInstances);
20
+ const newInstance = !this._context || this._context !== context;
21
+ if (!newInstance) {
22
+ const instance = cachedInstances
23
+ .find(service => this._cacheMatch(service, context, attributes));
24
+ if (instance)
25
+ return instance;
26
+ }
10
27
  const instance = { context };
11
28
  Object.setPrototypeOf(instance, this);
29
+ cachedInstances.push(instance);
12
30
  return instance;
13
31
  }
32
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
33
+ _cacheMatch(service, context, attributes) {
34
+ return service.context === context;
35
+ }
14
36
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@opra/core",
3
- "version": "0.31.5",
3
+ "version": "0.31.6",
4
4
  "description": "Opra schema package",
5
5
  "author": "Panates",
6
6
  "license": "MIT",
@@ -29,7 +29,7 @@
29
29
  },
30
30
  "dependencies": {
31
31
  "@browsery/type-is": "^0.6.3",
32
- "@opra/common": "^0.31.5",
32
+ "@opra/common": "^0.31.6",
33
33
  "accepts": "^1.3.8",
34
34
  "content-disposition": "^0.5.4",
35
35
  "content-type": "^1.0.5",
@@ -2,7 +2,7 @@ import { RequestContext } from '../request-context.js';
2
2
  export declare abstract class ApiService {
3
3
  protected _context: RequestContext;
4
4
  get context(): RequestContext;
5
- forContext(context: RequestContext, options?: {
6
- newInstance?: boolean;
7
- }): this;
5
+ forContext(source: ApiService): this;
6
+ forContext(context: RequestContext, attributes?: any): this;
7
+ protected _cacheMatch(service: ApiService, context: RequestContext, attributes?: any): boolean;
8
8
  }