1 | 'use strict'
|
2 |
|
3 | var mod$15 = require('./signals');var ACQUIRE = mod$15.ACQUIRE;var RELEASE = mod$15.RELEASE;var DISPOSE = mod$15.DISPOSE;
|
4 |
|
5 | module.exports = function singletonify(create, LAZY) {
|
6 |
|
7 | LAZY = LAZY || false
|
8 |
|
9 | return function (value, resolveDeps, releaseDeps) {
|
10 |
|
11 | var handlers = {}
|
12 | var count = 0
|
13 | var instance
|
14 |
|
15 | function dispose() {
|
16 | if (typeof instance.dispose === 'function') {
|
17 | instance.dispose()
|
18 | }
|
19 | instance = undefined
|
20 | count = 0
|
21 | }
|
22 |
|
23 | handlers[ACQUIRE] = function (value) {
|
24 | if (typeof instance === 'undefined') {
|
25 | instance = create.call(this, value, resolveDeps())
|
26 | }
|
27 | count++
|
28 | return instance
|
29 | }
|
30 |
|
31 | handlers[RELEASE] = function (value) {
|
32 | count--
|
33 | if (count == 0 && !LAZY) {
|
34 | releaseDeps()
|
35 | dispose()
|
36 | }
|
37 | }
|
38 |
|
39 | handlers[DISPOSE] = function (value) {
|
40 | if (instance) {
|
41 | releaseDeps()
|
42 | dispose()
|
43 | }
|
44 | }
|
45 |
|
46 | return function (signal) {
|
47 | return handlers[signal].call(this, value)
|
48 | }
|
49 | }
|
50 | } |
\ | No newline at end of file |