From 33058f2b292b3a581333bdfb21b8f671898c5060 Mon Sep 17 00:00:00 2001 From: Peter Bengtsson Date: Tue, 8 Dec 2020 14:40:17 -0500 Subject: initial commit --- .../finalizationregistry/cleanupsome/index.html | 74 ++++++++++ .../finalizationregistry/index.html | 66 +++++++++ .../global_objects/finalizationregistry/index.html | 152 +++++++++++++++++++++ .../finalizationregistry/register/index.html | 80 +++++++++++ .../finalizationregistry/unregister/index.html | 134 ++++++++++++++++++ 5 files changed, 506 insertions(+) create mode 100644 files/ja/web/javascript/reference/global_objects/finalizationregistry/cleanupsome/index.html create mode 100644 files/ja/web/javascript/reference/global_objects/finalizationregistry/finalizationregistry/index.html create mode 100644 files/ja/web/javascript/reference/global_objects/finalizationregistry/index.html create mode 100644 files/ja/web/javascript/reference/global_objects/finalizationregistry/register/index.html create mode 100644 files/ja/web/javascript/reference/global_objects/finalizationregistry/unregister/index.html (limited to 'files/ja/web/javascript/reference/global_objects/finalizationregistry') diff --git a/files/ja/web/javascript/reference/global_objects/finalizationregistry/cleanupsome/index.html b/files/ja/web/javascript/reference/global_objects/finalizationregistry/cleanupsome/index.html new file mode 100644 index 0000000000..c25d3dd476 --- /dev/null +++ b/files/ja/web/javascript/reference/global_objects/finalizationregistry/cleanupsome/index.html @@ -0,0 +1,74 @@ +--- +title: FinalizationRegistry.prototype.cleanupSome() +slug: Web/JavaScript/Reference/Global_Objects/FinalizationRegistry/cleanupSome +tags: + - FinalizationRegistry + - JavaScript + - Method + - Prototype + - Reference +translation_of: Web/JavaScript/Reference/Global_Objects/FinalizationRegistry/cleanupSome +--- +
{{JSRef}}
+ +

cleanupSome() メソッドは、 {{jsxref("FinalizationRegistry")}} 内のオブジェクトのうち、まだコールバックが呼ばれていないが回収されているオブジェクトの数が実装で選択された場合に、クリーンアップコールバックを起動します。このメソッドはオプションです。

+ +

構文

+ +
registry.cleanupSome([callback]);
+
+ +

引数

+ +
+
callback {{optional_inline}}
+
この cleanupSome への呼び出しによって起動されるコールバックだけに使用するコールバックを指定します。指定した場合、このコールバックは FinalizationRegistry で作成されたものの代わりに使用されます。
+
+ +

返値

+ +

undefined です。

+ +

+ +

通常、この関数を呼び出すことはありません。コールバックを適切に行うためには、 JavaScript エンジンのガベージコレクターに任せてください。この関数は主に、イベントループを発生させない、通常の JavaScript コードよりも WebAssembly で出てくる可能性の高い、長期に実行されるコードに対応するために存在します。また、コールバックが呼び出されない場合があることにも注意してください (例えば、ターゲットが回収されたレジストリ項目が存在しない場合)。

+ +

レジストリからクリーンアップされる (クリーンアップコールバックを呼び出す) 回収オブジェクトの項目数は、実装で定義されています。実装によっては、対象となる項目を一つだけ削除したり、対象となるすべての項目を削除したり、あるいはその間のどこかで削除したりすることもあります。

+ +

+ +

cleanupSome の使用

+ +
registry.cleanupSome?.(heldValue => {
+  // ...
+});
+
+ +

このメソッドはオプションであり、実装によってはこのメソッドを持たない場合があります。詳細は HTML issue #5446 を参照してください。このメソッドはオプションなので、それを呼び出す前にそのメソッドが存在することを確認する必要があります。そのための一つの方法は、上の例のようにオプション連結 (?.) を使用することです。

+ +

仕様書

+ + + + + + + + + + + + +
仕様書
{{SpecName('WeakRefs', '#sec-finalization-registry.prototype.cleanupSome', 'FinalizationRegistry.prototype.cleanupSome')}}
+ +

ブラウザーの互換性

+ + + +

{{Compat("javascript.builtins.FinalizationRegistry.cleanupSome")}}

+ +

関連情報

+ + diff --git a/files/ja/web/javascript/reference/global_objects/finalizationregistry/finalizationregistry/index.html b/files/ja/web/javascript/reference/global_objects/finalizationregistry/finalizationregistry/index.html new file mode 100644 index 0000000000..e8ca492f4d --- /dev/null +++ b/files/ja/web/javascript/reference/global_objects/finalizationregistry/finalizationregistry/index.html @@ -0,0 +1,66 @@ +--- +title: FinalizationRegistry() コンストラクター +slug: >- + Web/JavaScript/Reference/Global_Objects/FinalizationRegistry/FinalizationRegistry +tags: + - Constructor + - FinalizationRegistry + - JavaScript + - Reference + - コンストラクター +translation_of: >- + Web/JavaScript/Reference/Global_Objects/FinalizationRegistry/FinalizationRegistry +--- +
{{JSRef}}
+ +

FinalizationRegistry コンストラクターは、指定されたコールバックを使用する {{jsxref("FinalizationRegistry")}} オブジェクトを生成します。

+ +

構文

+ +
new FinalizationRegistry([callback]);
+
+ +

引数

+ +
+
callback {{optional_inline}}
+
レジストリが使用するコールバック関数です。指定された場合、関数である必要があります。
+
+ +

+ +

新しいレジストリの生成

+ +

コールバックを渡してレジストリを生成します。

+ +
const registry = new FinalizationRegistry(heldValue => {
+  // ....
+});
+
+ +

仕様書

+ + + + + + + + + + + + +
仕様書
{{SpecName('WeakRefs', '#sec-finalization-registry-constructor', 'FinalizationRegistry constructor')}}
+ +

ブラウザーの互換性

+ + + +

{{Compat("javascript.builtins.FinalizationRegistry.FinalizationRegistry")}}

+ +

関連情報

+ + diff --git a/files/ja/web/javascript/reference/global_objects/finalizationregistry/index.html b/files/ja/web/javascript/reference/global_objects/finalizationregistry/index.html new file mode 100644 index 0000000000..0277d4bde9 --- /dev/null +++ b/files/ja/web/javascript/reference/global_objects/finalizationregistry/index.html @@ -0,0 +1,152 @@ +--- +title: FinalizationRegistry +slug: Web/JavaScript/Reference/Global_Objects/FinalizationRegistry +tags: + - Class + - FinalizationRegistry + - JavaScript + - Reference +translation_of: Web/JavaScript/Reference/Global_Objects/FinalizationRegistry +--- +
{{JSRef}}
+ +

FinalizationRegistry オブジェクトにより、オブジェクトがガベージコレクションで回収されるときにコールバックを要求することができます。

+ +

解説

+ +

FinalizationRegistry は、レジストリに登録されているオブジェクトが回収される (ガベージコレクションされる) 時にクリーンアップコールバックを要求する方法を提供します。(クリーンアップコールバックはファイナライザーと呼ばれることもあります。)

+ +
+

注: Cleanup callbacks should not be used for essential program logic. 詳しくは Notes on cleanup callbacks を見てください。

+
+ +

コールバックで渡すレジストリを作成します。

+ +
const registry = new FinalizationRegistry(heldValue => {
+  // ....
+});
+
+ +

次に、 register メソッドを呼び出して、クリーンアップコールバックを行いたいオブジェクトを登録し、そのオブジェクトと保持値を渡します。

+ +
registry.register(theObject, "some value");
+
+ +

レジストリがオブジェクトへの強い参照を保持すると、目的に反してしまうので、 (レジストリが強い参照を保持していれば、そのオブジェクトは決して回収されない)、強い参照は保持はしません。

+ +

theObject が回収された場合、クリーンアップコールバックは、指定した保持値 (上では "some value") で呼び出される可能性があります。保持値は、プリミティブでもオブジェクトでも、 undefined であっても構いません。保持値がオブジェクトの場合、レジストリはその値への強い参照を保持します (これにより、後でクリーンアップコールバックに渡すことができます)。

+ +

オブジェクトの登録を解除したい場合は、三番目の値を渡します。 これは、後でレジストリのレジストリの unregister 関数をコールしてオブジェクトの登録を解除する際に使用する登録解除トークンです。レジストリは、登録解除トークンへの弱い参照のみを保持します。

+ +

よくオブジェクト自身が登録解除トークンとして使われ、これは良い結果になります。

+ +
registry.register(theObject, "some value", theObject);
+// ...some time later, if you don't care about `theObject` anymore...
+registry.unregister(theObject);
+
+ +

ただし、同じオブジェクトである必要はありません。異なるものでも構いません。

+ +
registry.register(theObject, "some value", tokenObject);
+// ...some time later, if you don't care about `theObject` anymore...
+registry.unregister(tokenObject);
+
+ +

コンストラクター

+ +
+
{{jsxref("FinalizationRegistry/FinalizationRegistry", "FinalizationRegistry()")}}
+
新しい FinalizationRegistry オブジェクト生成します。
+
+ +

インスタンスメソッド

+ +
+
{{jsxref("FinalizationRegistry.register", "FinalizationRegistry.prototype.register()")}}
+
Registers an object with the registry in order to get a cleanup callback when/if the object is garbage-collected.
+
{{jsxref("FinalizationRegistry.unregister", "FinalizationRegistry.prototype.unregister()")}}
+
Unregisters an object from the registry.
+
{{jsxref("FinalizationRegistry.cleanupSome", "FinalizationRegistry.prototype.cleanupSome()")}}
+
(Optional method.) Proactively requests cleanup callbacks for reclaimed objects.
+
+ +

Avoid where possible

+ +

Correct use of FinalizationRegistry takes careful thought, and it's best avoided if possible. It's also important to avoid relying on any specific behaviors not guaranteed by the specification. When, how, and whether garbage collection occurs is down to the implementation of any given JavaScript engine. Any behavior you observe in one engine may be different in another engine, in another version of the same engine, or even in a slightly different situation with the same version of the same engine. Garbage collection is a hard problem that JavaScript engine implementers are constantly refining and improving their solutions to.

+ +

Here are some specific points that the authors of the WeakRef proposal that FinalizationRegistry is part of included in its explainer document:

+ +
+

Garbage collectors are complicated. If an application or library depends on GC cleaning up a FinalizationRegistry or calling a finalizer [cleanup callback] in a timely, predictable manner, it's likely to be disappointed: the cleanup may happen much later than expected, or not at all. Sources of variability include:

+ + +
+ +

Notes on cleanup callbacks

+ +

Some notes on cleanup callbacks:

+ + + +

+ +

Creating a new registry

+ +

You create the registry passing in the callback:

+ +
const registry = new FinalizationRegistry(heldValue => {
+  // ....
+});
+
+ +

Registering objects for cleanup

+ +

Then you register any objects you want a cleanup callback for by calling the `register` method, passing in the object and a *held value* for it:

+ +
registry.register(theObject, "some value");
+ +

仕様書

+ + + + + + + + + + + + +
仕様書
{{SpecName('WeakRefs', '#sec-finalization-registry-objects', 'FinalizationRegistry')}}
+ +

ブラウザーの互換性

+ + + +

{{Compat("javascript.builtins.FinalizationRegistry")}}

+ +

関連情報

+ + diff --git a/files/ja/web/javascript/reference/global_objects/finalizationregistry/register/index.html b/files/ja/web/javascript/reference/global_objects/finalizationregistry/register/index.html new file mode 100644 index 0000000000..fc8fbe9475 --- /dev/null +++ b/files/ja/web/javascript/reference/global_objects/finalizationregistry/register/index.html @@ -0,0 +1,80 @@ +--- +title: FinalizationRegistry.prototype.register() +slug: Web/JavaScript/Reference/Global_Objects/FinalizationRegistry/register +tags: + - FinalizationRegistry + - JavaScript + - Method + - Prototype + - Reference + - メソッド +translation_of: Web/JavaScript/Reference/Global_Objects/FinalizationRegistry/register +--- +
{{JSRef}}
+ +

register() メソッドはオブジェクトを {{jsxref("FinalizationRegistry")}} インスタンスへ登録し、オブジェクトがガベージコレクションで回収された場合、レジストリのコールバックが呼び出されるようになります。

+ +

構文

+ +
registry.register(target, heldValue, [unregisterToken]);
+
+ +

引数

+ +
+
target
+
登録する対象オブジェクトです。
+
heldValue
+
このオブジェクトのためにファイナライザーへ渡す値です。これは target オブジェクトにすることはできません。
+
unregisterToken {{optional_inline}}
+
のちに対象オブジェクトを登録解除するために unregister メソッドと共に使用するトークンです。提供された場合 (かつ undefined ではない場合)、これはオブジェクトである必要があります。提供されなかった場合は、対象を登録解除することはできません。
+
+ +

返値

+ +

undefined です。

+ +

+ +

重要な注意点については、 {{jsxref("FinalizationRegistry")}} ページの可能な限り避けるクリーンアップコールバックに関する注意事項の部分を参照してください。

+ +

+ +

register の使用

+ +

次の例は target で参照される対象オブジェクトを保持値 "some value" で登録し、対象オブジェクト自身を登録解除トークンとして渡します。

+ +
registry.register(target, "some value", target);
+
+ +

次の例は target で参照される対象オブジェクトを他のオブジェクトを保持値として登録し、登録解除トークンを渡していません (すなわち target は登録解除できません)。

+ +
registry.register(target, {"useful": "info about target"});
+
+ +

仕様書

+ + + + + + + + + + + + +
仕様書
{{SpecName('WeakRefs', '#sec-finalization-registry.prototype.register', 'FinalizationRegistry.prototype.register')}}
+ +

ブラウザーの互換性

+ + + +

{{Compat("javascript.builtins.FinalizationRegistry.register")}}

+ +

関連情報

+ + diff --git a/files/ja/web/javascript/reference/global_objects/finalizationregistry/unregister/index.html b/files/ja/web/javascript/reference/global_objects/finalizationregistry/unregister/index.html new file mode 100644 index 0000000000..3b2245b146 --- /dev/null +++ b/files/ja/web/javascript/reference/global_objects/finalizationregistry/unregister/index.html @@ -0,0 +1,134 @@ +--- +title: FinalizationRegistry.prototype.unregister() +slug: Web/JavaScript/Reference/Global_Objects/FinalizationRegistry/unregister +tags: + - FinalizationRegistry + - JavaScript + - Method + - Prototype + - Reference + - メソッド +translation_of: Web/JavaScript/Reference/Global_Objects/FinalizationRegistry/unregister +--- +
{{JSRef}}
+ +

unregister は対象のオブジェクトを {{jsxref("FinalizationRegistry")}} インスタンスから登録解除します。

+ +

構文

+ +
registry.unregister(unregisterToken);
+
+ +

引数

+ +
+
unregisterToken
+
対象オブジェクトを登録したときに {{jsxref("FinalizationRegistry.prototype.register", "register")}} メソッドで使用したトークンです。
+
+ +

返値

+ +

undefined です。

+ +

+ +

対象オブジェクトの回収が完了すると、レジストリに登録された状態ではなくなります。クリーンアップコールバックですべてに unregister を行う必要はありません。クリーンアップコールバックを受信しておらず、クリーンアップコールバックを受信する必要がなくなった場合にのみ unregister を呼び出してください。

+ +

+ +

unregister の使用

+ +

この例では、登録解除トークンとして同じオブジェクトを使用して対象のオブジェクトを登録し、その後、 unregister を介して対象のオブジェクトの登録を解除します。

+ +
class Thingy {
+    #cleanup = label => {
+    //         ^^^^^−−−−− held value
+        console.error(
+            `The \`release\` method was never called for the object with the label "${label}"`
+        );
+    };
+    #registry = new FinalizationRegistry(this.#cleanup);
+
+    /**
+     * Constructs a `Thingy` instance. Be sure to call `release` when you're done with it.
+     *
+     * @param   label       A label for the `Thingy`.
+     */
+    constructor(label) {
+        //                            vvvvv−−−−− held value
+        this.#registry.register(this, label, this);
+        //          target −−−−−^^^^         ^^^^−−−−− unregister token
+    }
+
+    /**
+     * Releases resources held by this `Thingy` instance.
+     */
+    release() {
+        this.#registry.unregister(this);
+        //                        ^^^^−−−−− unregister token
+    }
+}
+
+ +

この例では、登録解除トークンとして別のオブジェクトを使用して対象のオブジェクトを登録しています。

+ +
 {
+    //         ^^^^−−−−− held value
+        console.error(
+            `The \`release\` method was never called for the \`Thingy\` for the file "${file.name}"`
+        );
+    };
+    #registry = new FinalizationRegistry(this.#cleanup);
+
+    /**
+     * Constructs a `Thingy` instance for the given file. Be sure to call `release` when you're done with it.
+     *
+     * @param   filename    The name of the file.
+     */
+    constructor(filename) {
+        this.#file = File.open(filename);
+        //                            vvvvv−−−−− held value
+        this.#registry.register(this, label, this.#file);
+        //          target −−−−−^^^^         ^^^^^^^^^^−−−−− unregister token
+    }
+
+    /**
+     * Releases resources held by this `Thingy` instance.
+     */
+    release() {
+        if (this.#file) {
+            this.#registry.unregister(this.#file);
+            //                        ^^^^^^^^^^−−−−− unregister token
+            File.close(this.#file);
+            this.#file = null;
+        }
+    }
+}
+
+ +

仕様書

+ + + + + + + + + + + + +
仕様書
{{SpecName('WeakRefs', '#sec-finalization-registry.prototype.unregister', 'FinalizationRegistry.prototype.unregister')}}
+ +

ブラウザーの互換性

+ + + +

{{Compat("javascript.builtins.FinalizationRegistry.unregister")}}

+ +

関連情報

+ + -- cgit v1.2.3-54-g00ecf